﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Text.RegularExpressions;
/*
Copyright 2010 Mirosław M. Mączka

This file is part of RegularExpressionTest.

    Flac2Wma is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace RegularExpressionTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //
        //http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
        //
        Regex r;
        Match m;

        public MainWindow()
        {
            InitializeComponent();
            //
            this.AddHandler(ListBox.MouseWheelEvent, new MouseWheelEventHandler(control_MouseWheel), true);
        }

        private void Window_SizeChanged_1(object sender, SizeChangedEventArgs e)
        {
            //
            double c = 100.0 + 100.0 + 50.0 + 40.0;
            double d = window1.ActualHeight - c;
            //window1.Title = string.Format("H:{0}, {1}", window1.Height, d);
            if (d > 10)
            {
                rowListBox.Height = new GridLength(d);
            }
            //
        }

        private void buttonMatch_Click(object sender, RoutedEventArgs e)
        {
            textBoxCount.Content = "";
            textBoxCount.ToolTip = "";
            listBoxGroups.BeginInit();
            listBoxGroups.Items.Clear();
            listBoxGroups.EndInit();
            //
            //@"(\\)(.*)(_App)"
            try
            {
                r = new Regex(textBoxPattern.Text.Trim(), RegexOptions.IgnoreCase);
                m = r.Match(textBoxInput.Text.Trim());
                if (m.Success)
                {
                    textBoxCount.Background = Brushes.LightGreen;
                    textBoxCount.Content = string.Format("Groups.Count={0}", m.Groups.Count);
                    textBoxCount.ToolTip = "";
                    //
                    int n = 0;
                    foreach (Group g in m.Groups)
                    {
                        listBoxGroups.BeginInit();
                        listBoxGroups.Items.Add((object)string.Format("{0}:[{1}]", n, g.Value));
                        listBoxGroups.EndInit();
                        n++;
                    }
                }
                else
                {
                    textBoxCount.Background = Brushes.LightPink;
                    textBoxCount.Content = "* Not Match *";
                    textBoxCount.ToolTip = "";
                }
                //
            }
            catch (Exception ex)
            {
                textBoxCount.Background = Brushes.LightPink;
                textBoxCount.Content = "* Error *";
                //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                textBoxCount.ToolTip = ex.Message;
            }
        }

        private void listBoxGroups_MouseMove(object sender, MouseEventArgs e)
        {
            ListBox lb = sender as ListBox;
            if (null == lb) return;
            //
            if (0 == lb.Items.Count)
            {
                lb.ToolTip = "";
                return;
            }
            //var aaa = ((ListBox)sender).InputHitTest(e.GetPosition(this));
            Point pt = e.GetPosition((UIElement)sender);
            var li = GetObjectDataFromPoint(lb, pt);
            if (null == li) return;
            //
            var ccc = ((ListBoxItem)li).Content;
            //
            ToolTip tt = new System.Windows.Controls.ToolTip();
            tt.FontSize = listBoxGroups.FontSize;
            tt.Background = Brushes.PowderBlue;
            tt.Content = ccc.ToString();
            listBoxGroups.ToolTip = tt;
        }

        private object GetObjectDataFromPoint(ListBox source, Point point)
        {
            // get the element under the mouse
            UIElement element = source.InputHitTest(point) as UIElement;

            if (element != null)
            {
                // if listbox item right off the bat, return it
                if (element is ListBoxItem)
                    return element;

                // otheriwse, loop up the  visual tree until a listboxitem is found
                // if we get to the listbox, no need to go on, no listbox item was found
                while (element != source)
                {
                    // get the element's parent
                    element = VisualTreeHelper.GetParent(element) as UIElement;

                    // if now a listboxitem, return it
                    if (element is ListBoxItem)
                        return element;
                }
            }

            return null;
        }

        private void Grid_ContextMenuOpening_1(object sender, ContextMenuEventArgs e)
        {
            var g = sender as Grid;
            if (null == g) return;
            //
            string s = e.Source.ToString();
            string os = e.OriginalSource.ToString();
            //
            if (s.Contains("ListBox") ||  os.Contains("StackPanel"))
            {
                ((MenuItem)(g).ContextMenu.Items[0]).IsEnabled = true;
                ((MenuItem)(g).ContextMenu.Items[1]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[2]).IsEnabled = true;
                ((MenuItem)(g).ContextMenu.Items[4]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[5]).IsEnabled = false;
            }
            else if (os.Contains("TextBoxView"))
            {
                ((MenuItem)(g).ContextMenu.Items[0]).IsEnabled = true;
                ((MenuItem)(g).ContextMenu.Items[1]).IsEnabled = true;
                ((MenuItem)(g).ContextMenu.Items[2]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[4]).IsEnabled = true;
                ((MenuItem)(g).ContextMenu.Items[5]).IsEnabled = true;
            }
            else
            {
                ((MenuItem)(g).ContextMenu.Items[0]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[1]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[2]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[4]).IsEnabled = false;
                ((MenuItem)(g).ContextMenu.Items[5]).IsEnabled = false;
            }
            

        }

        private void copyMenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            while (item.Parent is MenuItem)
            {
                item = (MenuItem)item.Parent;
            }
            var menu = item.Parent as ContextMenu;
            if (menu == null) return;
            //
            if(menu.PlacementTarget is TextBox)
            {
                var tb = menu.PlacementTarget as TextBox;
                string s = tb.Text.Trim();
                if("" != s)
                {
                    Clipboard.SetText(s, TextDataFormat.UnicodeText);
                }
                e.Handled = true;
            }
            else if (menu.PlacementTarget is ListBox)
            {
                var lb = menu.PlacementTarget as ListBox;
                if (0 != lb.SelectedItems.Count)
                {
                    Clipboard.SetText(lb.SelectedItems[0].ToString(), TextDataFormat.UnicodeText);
                }
                e.Handled = true;
            }
            //
        }

        private void pasteMenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            while (item.Parent is MenuItem)
            {
                item = (MenuItem)item.Parent;
            }
            var menu = item.Parent as ContextMenu;
            if (menu == null) return;
            //
            if (menu.PlacementTarget is TextBox)
            {
                var tb = menu.PlacementTarget as TextBox;
                tb.Text = Clipboard.GetText(TextDataFormat.UnicodeText).Trim();
                e.Handled = true;
            }
            else if (menu.PlacementTarget is ListBox)
            {
                //var lb = menu.PlacementTarget as ListBox;
                e.Handled = true;
            }
            //
        }

        private void copyAll_Click_1(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            while (item.Parent is MenuItem)
            {
                item = (MenuItem)item.Parent;
            }
            var menu = item.Parent as ContextMenu;
            if (menu == null) return;
            if (menu.PlacementTarget is ListBox)
            {
                string s = "";
                for(int i=0; i< listBoxGroups.Items.Count; i++)
                {
                    s += (listBoxGroups.Items[i].ToString() + Environment.NewLine);
                }
                if ("" != s)
                {
                    Clipboard.SetText(s, TextDataFormat.UnicodeText);
                }
                e.Handled = true;
            }
            //
        }

        private void copyTemplate_Click_1(object sender, RoutedEventArgs e)
        {
            //if("" != textBoxPattern.Text.Trim())
                Util.CopyTemplateToClipborad(textBoxPattern.Text);
            e.Handled = true;
        }

        private void rmEscMenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            while (item.Parent is MenuItem)
            {
                item = (MenuItem)item.Parent;
            }
            var menu = item.Parent as ContextMenu;
            if (menu == null) return;
            //
            if (menu.PlacementTarget is TextBox)
            {
                var tb = menu.PlacementTarget as TextBox;
                tb.Text = tb.Text.Replace("\\\"", "\"");
                e.Handled = true;
            }
            //
        }

        private void addEscMenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var item = sender as MenuItem;
            while (item.Parent is MenuItem)
            {
                item = (MenuItem)item.Parent;
            }
            var menu = item.Parent as ContextMenu;
            if (menu == null) return;
            //
            if (menu.PlacementTarget is TextBox)
            {
                var tb = menu.PlacementTarget as TextBox;
                tb.Text = tb.Text.Replace("\"", "\\\"");
                e.Handled = true;
            }
            //
        }

        private void listBoxGroups_MouseLeave(object sender, MouseEventArgs e)
        {
            listBoxGroups.ToolTip = "";
        }

        private bool handleClipborad(TextBox tb, Key k)
        {
            if (k == Key.C)
            {
                string s = tb.Text.Trim();
                if ("" != s)
                {
                    Clipboard.SetText(s, TextDataFormat.UnicodeText);
                }
                return true;
            }
            else if (k == Key.X)
            {
                string s = tb.Text.Trim();
                if ("" != s)
                {
                    Clipboard.SetText(s, TextDataFormat.UnicodeText);
                    tb.Text = "";
                }
                return true;
            }
            else if (k == Key.V)
            {
                tb.Text = Clipboard.GetText(TextDataFormat.UnicodeText).Trim();
                return true;
            }
            return false;
        }

        private void textBoxPattern_KeyUp(object sender, KeyEventArgs e)
        {
            return;

            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (e.Key == Key.C)
                {
                    string s = textBoxPattern.Text.Trim();
                    if ("" != s)
                    {
                        Clipboard.SetText(s, TextDataFormat.UnicodeText);
                    }
                    e.Handled = true;
                }
                else if (e.Key == Key.X)
                {
                    string s = textBoxPattern.Text.Trim();
                    if ("" != s)
                    {
                        Clipboard.SetText(s, TextDataFormat.UnicodeText);
                        textBoxPattern.Text = "";
                    }
                    e.Handled = true;
                }
                else if (e.Key == Key.V)
                {
                    textBoxPattern.Text = Clipboard.GetText(TextDataFormat.UnicodeText).Trim();
                    e.Handled = true;
                }
            }
        }

        private void textBoxInput_KeyUp(object sender, KeyEventArgs e)
        {
            return;
            //
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                if (e.Key == Key.C)
                {
                    string s = textBoxInput.Text.Trim();
                    if ("" != s)
                    {
                        Clipboard.SetText(s, TextDataFormat.UnicodeText);
                    }
                    e.Handled = true;
                }
                else if (e.Key == Key.X)
                {
                    string s = textBoxInput.Text.Trim();
                    if ("" != s)
                    {
                        Clipboard.SetText(s, TextDataFormat.UnicodeText);
                        textBoxInput.Text = "";
                    }
                    e.Handled = true;
                }
                else if (e.Key == Key.V)
                {
                    textBoxInput.Text = Clipboard.GetText(TextDataFormat.UnicodeText).Trim();
                    e.Handled = true;
                }
            }
        }

        private bool handleWheel(Control c, int delta)
        {

            if (delta > 0)
            {
                double d = c.FontSize + 2;
                c.FontSize = Math.Min(Math.Max(d, window1.FontSize), 30);
            }
            else
            {
                double d = c.FontSize - 2;
                c.FontSize = Math.Min(Math.Max(d, window1.FontSize), 30);
            }
            return true;
        }

        private void control_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //
            if (Keyboard.Modifiers != ModifierKeys.Control) return;
            //
            if (e.Source is TextBox) handleWheel((Control)e.Source, e.Delta);
            else if ((e.Source is StackPanel) || (e.Source is ListBox)) handleWheel(listBoxGroups, e.Delta);
            e.Handled = true;
        }

        private void textBoxPattern_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void textBoxPattern_DragLeave(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.None;
        }

        private void textBoxPattern_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void textBoxPattern_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
                string d = ((string)e.Data.GetData(DataFormats.UnicodeText));
                if (string.IsNullOrEmpty(d)) return;
                //
                textBoxPattern.Text = d.Trim();
            }
        }

        private void textBoxInput_DragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void textBoxInput_DragLeave(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.None;
        }

        private void textBoxInput_DragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.Copy;
        }

        private void textBoxInput_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
            {
                e.Effects = DragDropEffects.Copy;
                string d = ((string)e.Data.GetData(DataFormats.UnicodeText));
                if (string.IsNullOrEmpty(d)) return;
                //
                textBoxInput.Text = d.Trim();
            }
        }





    }
}
